/*
 * @Author: your name
 * @Date: 2021-03-29 14:23:41
 * @LastEditTime: 2021-03-31 20:16:40
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \shop\training-group-5-warehouse\dianshang\src\store\bargain.js
 */
/*
 * @Author: your name
 * @Date: 2021-03-29 14:23:41
 * @LastEditTime: 2021-03-29 14:25:10
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \shop\training-group-5-warehouse\dianshang\src\store\bargain.js
 */
//砍价
export default {
    namespaced: true,
    state: {
        userCity: [],//收货人地址
        shopItem: JSON.parse(localStorage.getItem("shopItem")) || {},//点击砍价的商品
        userBargainRecord: [],//用户砍价记录
        shopBargain: [],//存储进行免单的商品
        localhost: '',//好友砍价链接,
        texts: []//储存用户发送信息
    },
    mutations: {
        //收货人地址
        SET_USERCITY(state, actions) {
            state.userCity.unshift(actions);
        },
        //点击免单的商品
        SET_SHOPITEM(state, actions) {
            localStorage.setItem("shopItem", JSON.stringify(actions));
            state.shopItem = actions;
        },
        //添加进行免单的商品
        SET_SHOPBARGAIN(state, actions) {
            state.shopItem.underway = true;
            state.shopBargain.unshift({ ...state.shopItem, ...actions, time: 24 * 60 * 60 * 1000 });
        },
        //添加事件
        SET_TIME(state, actions = 24 * 60 * 60 * 1000) {
            const data = state.shopBargain.map((item, index) => {
                if (index === 0) {
                    item = { ...item, time: actions }
                }
                return item;
            });
            state.shopBargain = data;
        },
        //链接赋值
        SET_LOCALHOST(state, actions) {
            state.localhost = actions;
        },
        //添加消息
        SET_TEXTS(state, actions) {
            const avatar = JSON.parse(localStorage.getItem("userInfo")).avatar || "";
            state.texts.push({ actions, avatar });
        },
        //修改商品状态
        SET_SHOPBARGAINS(state, actions) {
            state.shopBargain.forEach(item => {
                if (item.shopId === state.shopItem.shopId && item.user === JSON.parse(localStorage.getItem("userInfo")).user) {
                    item.done = true;
                }
            });
        }
    },
    actions: {},
}